# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=opencamlib
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2023.01.11
pkgrel=4
pkgdesc='Multi-Purpose CNC Toolpath Library (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/aewallin/opencamlib'
license=('spdx:LGPL-2.1')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${url}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        '0001-ocl-2023.01.11-fix-cxx-destination.patch'
        '0002-ocl-2023.01.11-fix-py-destination.patch')
sha256sums=('07fd9b61c39c4d600e441d67edbafc1015e9e3fabd88b561a647f98fedf31d41'
            'c300e111929af47f63b820004b0d6ef02469280a58b5d8cf9097c47ba477a47c'
            'fe5f1fd6a9a2189f9d3427d8f049ff7b701ddf3ce4d16c6ba3eda9916830f880')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/"${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-ocl-2023.01.11-fix-cxx-destination.patch \
    0002-ocl-2023.01.11-fix-py-destination.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  rm -rf "${srcdir}-build-${MSYSTEM}-cxx" | true
  mkdir -p "${srcdir}/build-${MSYSTEM}-cxx" && cd "${srcdir}/build-${MSYSTEM}-cxx"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_CXX_LIB=ON \
    -DBUILD_PY_LIB=OFF \
    -DBUILD_DOC=OFF \
    -DUSE_OPENMP=ON \
    -DUSE_PY_3=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  rm -rf "${srcdir}-build-${MSYSTEM}-py" | true
  mkdir -p "${srcdir}/build-${MSYSTEM}-py" && cd "${srcdir}/build-${MSYSTEM}-py"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_CXX_LIB=OFF \
    -DBUILD_PY_LIB=ON \
    -DBUILD_DOC=OFF \
    -DUSE_OPENMP=ON \
    -DUSE_PY_3=ON \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python3.exe \
    -DBoost_USE_STATIC_LIBS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package_opencamlib() {
  cd "${srcdir}/build-${MSYSTEM}-cxx"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}

package_python-opencamlib() {
  pkgdesc+=" - Python bindings"
  depends+=("${MINGW_PACKAGE_PREFIX}-boost"
            "${MINGW_PACKAGE_PREFIX}-python")

  cd "${srcdir}/build-${MSYSTEM}-py"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;

# vim: ts=2 sw=2 et:
